package com.bupt.smart.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bupt.smart.smart.R;

import java.util.List;

/**
 * Created by ght on 2016/4/6.
 */
public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerViewAdapter.MyViewHolder> {
    public static final int ITEM_VIEW_TYPE_FIRST_LINE = 0;
    public static final int ITEM_VIEW_TYPE_OTHER_LINE = 1;
    private Context mContext;
    private List<ItemData> mData;

    private OnItemClickListener mListener;

    /**
     * 点击事件回调接口
     */
    public interface OnItemClickListener {

        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.mListener = listener;
    }

    public HomeRecyclerViewAdapter(Context context, List<ItemData> datas) {

        this.mContext = context;
        mData = datas;
    }

    //实例化ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.home_recyclerview_item, parent, false);

        return new MyViewHolder(view);
    }

    //绑定数据
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {


        System.out.println(position);
        holder.mImageView.setImageResource(mData.get(position).imgID);
        holder.mTextView.setText(mData.get(position).txt);

        if (mListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //获取实际的位置
                    int pos = holder.getLayoutPosition();

                    mListener.onItemClick(v, pos);

                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    int pos = holder.getLayoutPosition();

                    mListener.onItemLongClick(v, pos);

                    return true;  //屏蔽单击  false点击事件仍会响应
                }
            });

        }
    }

    @Override
    public int getItemCount() {

        return mData.size();

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == 1) {
            return ITEM_VIEW_TYPE_FIRST_LINE;
        } else {
            return ITEM_VIEW_TYPE_OTHER_LINE;
        }
    }

    /**
     * 在当前Item之前添加Item
     */
    public void addPreItem(ItemData data,int position) {

        mData.add(position,data);
        notifyItemInserted(position);
    }

    /**
     * 在当前Item之后添加Item
     */
    public void addNextItem(ItemData data,int position) {

        mData.add(position+1,data);
        notifyItemInserted(position+1);
    }

    /**
     * 删除Item
     * @param position
     */
    public void removeItem(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView mImageView;
        TextView mTextView;

        public MyViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.img_home_func);
            mTextView = (TextView) itemView.findViewById(R.id.txt_home_func);
        }
    }

    public static class ItemData {
        int imgID;
        String txt;

        public ItemData(int imgID, String txt) {
            this.imgID = imgID;
            this.txt = txt;
        }
    }

}
